//
//  FBtMessageModel.h
//  JwPackDemo
//
//  Created by 陈警卫 on 2025/8/14.
//

#import <Foundation/Foundation.h>

#import "FBtSignal.h"
#import "FBtExten.h"

@class CXBtPeriModel;

typedef NS_ENUM(NSUInteger, FBtMessageState) {
    FBtMessageStateInit,    // 默认
    FBtMessageStateSended,  // 发送
    FBtMessageStateReplyed, // 回复
    FBtMessageStateTimeout, // 超时
};

typedef NS_ENUM(NSUInteger, FBtMessageLevel) {
    FBtMessageLevelPlain, // 默认
    FBtMessageLevelPrior, // 优先
};

typedef NS_ENUM(NSUInteger, FBtMessageWriteType) {
    FBtMessageWriteTypeInit,
    FBtMessageWriteTypeWithoutResponse,
    FBtMessageWriteTypeResponse,
};

/**
 指令包 示例
 包头  功能码 时间戳  数据长度  值  CRC-16/MODBUS(反序) 包尾
 7F7F  3A  6881A533 0001   03        367B         F7F7
 */
/**
 
 F7F7 AA FBEB 0003 0000 D096 F7F7
 
 
 */

@interface FBtMessageModel : NSObject

// - 回调

/// 数据回调
@property (nonatomic, copy) void(^didReceiveAction)(FBtMessageModel *messageModel);

// - 配置

/// 发送状态
@property (nonatomic, assign) FBtMessageState state;
/// 发送级别
@property (nonatomic, assign) FBtMessageLevel level;
/// 响应类型
@property (nonatomic, assign) FBtMessageWriteType writeType;

@property (nonatomic, strong) CXBtPeriModel *periModel;

// - 收发

/// key
@property (nonatomic, strong) NSString *key;
/// signal
@property (nonatomic, assign) FBtSig signal;

/// 包头
@property (nonatomic, assign) UInt16 prefix;
/// 包尾
@property (nonatomic, assign) UInt16 suffix;

/// 功能码
@property (nonatomic, assign) UInt8 func;
/// 时间戳
@property (nonatomic, strong) NSData *stamp;
/// 数据长度
@property (nonatomic, assign) UInt16 length;
/// 类型码(值首位字节)
@property (nonatomic, assign) UInt8 type;
/// 数据值
@property (nonatomic, strong) NSData *value;
/// 完整数据
@property (nonatomic, strong) NSData *data;

/// 配置完整数据
- (void)setupCompleData;

/// 接收完整数据
@property (nonatomic, strong) NSData *r_data;
/// 接收数据值
@property (nonatomic, strong) NSData *r_value;
/// 接收数据解析
@property (nonatomic, strong) NSDictionary *r_decode;

// - 功能

/// 超时时间(默认3s)
@property (nonatomic, assign) CGFloat timeout;
/// 超时后重复次数(默认0)
@property (nonatomic, assign) NSUInteger echo;

// - 方法

/// 启动计时器
- (void)startTimerWithAction:(void(^)(FBtMessageModel *messageModel))action;

/// 发送数据
- (FBtMessageModel *)send;

@end

